﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SGArchiveSystem7x.Controllers.ArchiveJK
{
    public class NoSQController : BaseController
    {
        private string sControllerName = "";
        // GET: 没有授权的
        public ActionResult Index()
        {
            var n = this.VaildUser(sControllerName);
            if (n != null)
                return n;

            ///清除Page级的Session
            App_Code.CSession.Clear_PageSession(Session);


            var cColumns = Models.CColumnsModel.Create_NoSQColumns();
            ViewBag.Columns = cColumns;

            ViewBag.Name = "未授权处理";
            return View();
        }

        [HttpPost]
        public ActionResult Get_ajaxNoSQ(string type)
        {
            if (type == "-1")
                type = "";
            ///获取没有处理的错误信息
            var list = SGArchiveSystem7xDB.Linq.T_ArchiveJKDal.Get_ArchiveJK(2, type, true);
            var json = new
            {
                rows = list.ToArray(),
                total = list.Count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 处理数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Set_ajaxNoSQCL(string ids, string clyj)
        {
            ///将ID获取
            if (ids.EndsWith(","))
                ids = ids.TrimEnd(',');
            string[] strs = ids.Split(',');

            int[] ints = new int[strs.Length];
            for (int i = 0; i < strs.Length; i++)
                ints[i] = int.Parse(strs[i]);

            var user = App_Code.CSession.Get_User(Session);
            return SGArchiveSystem7xDB.Linq.T_ArchiveJKDal.Set_CLJK(ints, clyj, user.U_RNAME).ToString();
        }

        /// <summary>
        /// 未授权不在库房的进行入库操作
        /// </summary>
        /// <returns></returns>
        public ActionResult RK()
        {
            var n = this.VaildUser(sControllerName);
            if (n != null)
                return n;

            ///清除Page级的Session
            App_Code.CSession.Clear_PageSession(Session);

            var cColumns = Models.CColumnsModel.Create_NotKFColumns();
            ViewBag.Columns = cColumns;

            var listArchiveTree = SGArchiveSystem7xDB.Linq.T_ArchiveTreeDal.Get_ArchiveTree();
            ViewBag.ArchiveTree = listArchiveTree;

            ViewBag.Name = "手动入库";
            return View();
        }

        [HttpPost]
        public ActionResult Get_ajaxRK(string arname, string dh, string tm, string nd)
        {
            int count = 0;
            if (arname == "-1")
                arname = "";
            var list = SGArchiveSystem7xDB.Linq.T_ArchiveJKDal.Get_ArchiveAJ(arname, dh, tm, nd,out count, 0, SGArchiveSystem7x.App_Code.CWebCommons._iPageRows);
            List<string> list1 = new List<string>();
            list1.Add(arname);
            list1.Add(dh);
            list1.Add(tm);
            list1.Add(nd);
            App_Code.CSession.Add_QueryWhere(Session, list1);
            var json = new
            {
                rows = list.ToArray(),
                total = count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult Get_ajaxRKPager(int num, int size)
        {
            ///分页后的起始行
            int iStart = (num - 1) * size;
            var listWhere = SGArchiveSystem7x.App_Code.CSession.Get_QueryWhere(Session);
            int count = 0;
            var list = SGArchiveSystem7xDB.Linq.T_ArchiveJKDal.Get_ArchiveAJ(listWhere[0], listWhere[1], listWhere[2], listWhere[3],
                out count, 0, SGArchiveSystem7x.App_Code.CWebCommons._iPageRows);
            var json = new
            {
                rows = list.ToArray(),
                total = count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 手动入库
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Set_ajaxHandRK(string ids)
        {
            ///将ID获取
            if (ids.EndsWith(","))
                ids = ids.TrimEnd(',');
            string[] strs = ids.Split(',');

            int[] ints = new int[strs.Length];
            for (int i = 0; i < strs.Length; i++)
                ints[i] = int.Parse(strs[i]);

            var user = App_Code.CSession.Get_User(Session);
            return SGArchiveSystem7xDB.Linq.T_ArchiveJKDal.Set_ArchiveAJRKAndAddJK(ints, user.U_RNAME).ToString();
        }

    }


}